package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baiju.bjpopupwindow.view.BottomDialog;
import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.baiju.style.multipicker.HashMultiPickerView;
import com.baiju.style.multipicker.MultiPickerView;
import com.baiju.style.multipicker.config.MultiPickerConfig;
import com.baiju.style.multipicker.listener.OnHashMultiPickerItemClickListener;
import com.baiju.style.multipicker.listener.OnMultiPickerItemClickListener;
import com.baiju.style.multipicker.widget.bean.MultiItemBean;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.controller.AttrInfoController;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.controller.EnterPlatformInfoController;
import com.example.dangerouscargodriver.entry.CompanyDetailInfo;
import com.example.dangerouscargodriver.entry.ResourceTypeInfo3;
import com.example.dangerouscargodriver.entry.ResourceTypeInfo4;
import com.example.dangerouscargodriver.entry.TruckClassInfo;
import com.example.dangerouscargodriver.entry.TruckTypeInfo;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.utils.AppManager;
import com.example.dangerouscargodriver.utils.DisplayUtil;
import com.example.dangerouscargodriver.utils.ImageUtils;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.google.gson.Gson;
import com.parkingwang.keyboard.OnInputChangedListener;
import com.parkingwang.keyboard.PopupKeyboard;
import com.parkingwang.keyboard.view.InputView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CompanyAddTruckActivity extends HttpRequestActivity implements View.OnClickListener {

	private static final String TAG = "CompanyAddTruckActivity";
	private ActionBar mActionBar = null;
	private Button mActionCommit = null;

	private LinearLayout mAddContactExtra = null;
	private LinearLayout mContactExtra = null;
	private EditText mHeft = null;
	private EditText mContactInfo = null;
	private EditText mTelephoneInfo = null;
	private EditText mContactInfoExtra = null;
	private EditText mTelephoneInfoExtra = null;

	//
	private LinearLayout mCarTypeSelector = null;
	private LinearLayout mCarType = null;

	//
	private HashMap<String, ArrayList<MultiItemBean>> mHashMapCarTypeArray = null;
	private ArrayList<String> mHashKeyArray = null;
	private HashMultiPickerView mCarTypePickerView = null;

	//
	private LinearLayout mResourceTypeSelector = null;
	private LinearLayout mResourceType = null;

	//
	private ArrayList<MultiItemBean> mResourceTypeArray = null;
	private MultiPickerView mResourceTypePickerView = null;

	//用户操作结果集
	private List<Integer> mSelectTruckCid = null;
	private String mSelectorTruck_cidKey = null;

	//
	private List<Integer> mSelectResource = null;


    //*************************************传图像相关********************************/
	private static final int ACTION_TRANSPORT_LICENSE_OFFSET = 1000;
	private static final int ACTION_EMPLOYEE_LICENSE_OFFSET = 1001;
	private static final int ACTION_QUALIFICATION_CERTIFICATE_OFFSET = 1002;
	private static final int ACTION_DRIVER_LICENSE_OFFSET = 1003;

	//相册请求码
	private static final int ALBUM_REQUEST_CODE = 1;
	//相机请求码
	private static final int CAMERA_REQUEST_CODE = 2;

	private static final int SELECT_TRANSPORT_LICENSE = 0;
	private static final int SELECT_EMPLOYEE_LICENSE = 1;
	private static final int SELECT_QUALIFICATION_CERTIFICATE = 2;
	private static final int SELECT_DRIVER_LICENSE = 3;

	private LinearLayout mLayoutTransportLicense = null;
	private ImageView mIvTransportLicense = null;
	private ImageView mIvTransportLicenseLoading = null;
	private boolean mIvTransportLicenseUpload = false;

	private LinearLayout mLayoutEmployeeLicense = null;
	private ImageView mIvEmployeeLicense = null;
	private ImageView mIvEmployeeLicenseLoading = null;
	private boolean mIvEmployeeLicenseUpload = false;

	private LinearLayout mLayoutCertificate = null;
	private ImageView mIvQualificationCertificate = null;
	private ImageView mIvQualificationCertificateLoading = null;
	private boolean mIvQualificationCertificateUpload = false;

	private LinearLayout mLayoutDriverLicense = null;
	private ImageView mIvDriverLicense = null;
	private ImageView mIvDriverLicenseLoading = null;
	private boolean mIvDriverLicenseUpload = false;

	private BottomDialog mBottomDialog = null;

	//调用照相机返回图片文件
	private File mTempFile;

	private int mSelectType = SELECT_TRANSPORT_LICENSE;
	//
	private HashMap<String, String> mSelectPicture = null;


	private String mCid = null;
	private String mCname = null;
	private CompanyDetailInfo mCompanyDetailInfo =null;

	private InputView mInputView;
	private PopupKeyboard mPopupKeyboard;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.initHttpComponent(CompanyAddTruckActivity.class);
		AppManager.getAppManager().addActivity(this);

		setContentView(R.layout.activity_company_add_truck);
		if(getIntent().getStringExtra("cid") != null){
			mCid = getIntent().getStringExtra("cid");
			mCname = getIntent().getStringExtra("cname");
		}

		mAddContactExtra = findViewById(R.id.ic_add_item);
		mAddContactExtra.setOnClickListener(this);
		mContactExtra = findViewById(R.id.layout_add_contact_extra);
		mHeft = findViewById(R.id.ed_heft);
		mContactInfo = findViewById(R.id.ed_contact_person);
		mTelephoneInfo = findViewById(R.id.ed_telephone_no);
		mContactInfoExtra = findViewById(R.id.ed_contact_person_extra);
		mTelephoneInfoExtra = findViewById(R.id.ed_telephone_no_extra);

		mActionCommit = findViewById(R.id.btn_action_commit);
		mActionCommit.setOnClickListener(this);

		mActionBar = findViewById(R.id.actionBarCompanyAddTruck);
		mActionBar.setOnBackClickListener( new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		//
		mCarTypeSelector = findViewById(R.id.layout_car_type_select);
		mCarTypeSelector.setOnClickListener(this);
		mCarType = findViewById(R.id.layout_car_type);

		//
		mCarTypePickerView = new HashMultiPickerView();
		mHashMapCarTypeArray = new HashMap<>();
		mHashKeyArray = new ArrayList<>();
		initTruckClassInfo();
		mCarTypePickerView.init(this, mHashMapCarTypeArray, mHashKeyArray);

		//
		mResourceTypeSelector = findViewById(R.id.layout_resource_type_selector);
		mResourceTypeSelector.setOnClickListener(this);
		mResourceType = findViewById(R.id.layout_resource_type);
		//
		mResourceTypePickerView = new MultiPickerView();
		mResourceTypeArray = new ArrayList<>();
		mResourceTypePickerView.init(this, mResourceTypeArray);

		//
		mSelectPicture = new HashMap<>();

		mLayoutTransportLicense = findViewById(R.id.layout_transport_license);
		mIvTransportLicense = findViewById(R.id.iv_transport_license);
		mLayoutTransportLicense.setOnClickListener(this);
		mIvTransportLicenseLoading = findViewById(R.id.iv_transport_license_loading);

		mLayoutEmployeeLicense = findViewById(R.id.layout_employment_license);
		mIvEmployeeLicense = findViewById(R.id.iv_employment_license);
		mLayoutEmployeeLicense.setOnClickListener(this);
		mIvEmployeeLicenseLoading = findViewById(R.id.iv_employment_license_loading);

		mLayoutCertificate = findViewById(R.id.layout_qualification_certificate);
		mIvQualificationCertificate = findViewById(R.id.iv_qualification_certificate);
		mLayoutCertificate.setOnClickListener(this);
		mIvQualificationCertificateLoading = findViewById(R.id.iv_qualification_certificate_loading);

		mLayoutDriverLicense = findViewById(R.id.layout_driver_license);
		mIvDriverLicense = findViewById(R.id.iv_driver_license);
		mLayoutDriverLicense.setOnClickListener(this);
		mIvDriverLicenseLoading = findViewById(R.id.iv_driver_license_loading);

		mBottomDialog = new BottomDialog();
		mBottomDialog.setOnItemClickListener(new BottomDialog.OnItemClickListener() {
			@Override
			public void onCamera() {
				getPicFromCamera();
			}

			@Override
			public void onAlbum() {
				getPicFromAlbum();
			}

			@Override
			public void onCancel() {

			}
		});
		mInputView = findViewById(R.id.input_view);
		// 创建弹出键盘
		mPopupKeyboard = new PopupKeyboard(this);
		// 弹出键盘内部包含一个KeyboardView，在此绑定输入两者关联。
		mPopupKeyboard.attach(mInputView, this);
		mPopupKeyboard.getController().addOnInputChangedListener(new OnInputChangedListener() {
			@Override
			public void onChanged(String s, boolean b) {

			}

			@Override
			public void onCompleted(String s, boolean b) {
				mPopupKeyboard.dismiss(CompanyAddTruckActivity.this);
			}
		});

		executeSearchCompanyInfo();
	}

	private void initTruckClassInfo(){
		mHashMapCarTypeArray.clear();
		mHashKeyArray.clear();
		if(AttrInfoController.Instance().getTruckTypeArray() != null &&
				AttrInfoController.Instance().getTruckTypeArray().size() > 0){
			for(int index = 0; index < AttrInfoController.Instance().getTruckTypeArray().size(); index++){
				TruckTypeInfo truckTypeInfo = AttrInfoController.Instance().getTruckTypeArray().get(index);
				List<TruckClassInfo> list = truckTypeInfo.getList();
				ArrayList<MultiItemBean> arrayList = new ArrayList<>();
				for(int index2 = 0; index2 < list.size(); index2++) {
					MultiItemBean bean = new MultiItemBean();
					bean.setName(list.get(index2).getClassname());
					bean.setId(list.get(index2).getClassid());
					bean.setSelected("0");
					arrayList.add(bean);
				}
				mHashMapCarTypeArray.put(truckTypeInfo.getType(), arrayList);
				mHashKeyArray.add(truckTypeInfo.getType());
			}
		}
	}

	private void addCarTypeItem(String selectKey, List<Integer> select) {
		if(select == null){
			return;
		}

		mCarType.removeAllViews();
		LayoutInflater layoutInflater = LayoutInflater.from(getBaseContext());
		for(int index = 0; index < select.size(); index++){
			View itemLayout = layoutInflater.inflate(R.layout.item_search_bar, null);

			ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			params.setMargins(DisplayUtil.dp2px(getBaseContext(), 0),
					0,
					12,
					0);
			itemLayout.setLayoutParams(params);

			TextView text = itemLayout.findViewById(R.id.item_show_str);
			text.setText(mHashMapCarTypeArray.get(selectKey).get(select.get(index)).getName());
			itemLayout.setBackgroundResource(R.drawable.rounded_button);
			mCarType.addView(itemLayout);
		}
	}

	private void showCarTypeSelector(){
		MultiPickerConfig pickerConfig = new MultiPickerConfig.Builder()
				.title("选择类型")
				.setCustomItemLayout(R.layout.item_city)//自定义item的布局
				.setCustomItemTextViewId(R.id.item_city_name_tv)
				.couldSelectNum(1)
				.build();
		pickerConfig.setTitleBackgroundColorStr("#FFD401");

		mCarTypePickerView.setConfig(pickerConfig);
		mCarTypePickerView.setOnMultiItemClickListener(new OnHashMultiPickerItemClickListener() {
			public void onSelected(String selectKey, List<Integer> select) {
				Log.d(TAG, "selectKey: " + selectKey);
				Log.d(TAG, "select: " + select.toString());
				mSelectTruckCid = select;
				mSelectorTruck_cidKey = selectKey;
				addCarTypeItem(selectKey, select);
			}

			@Override
			public void onCancel() {
				/**/
			}
		});

		mCarTypePickerView.showHashMultiPicker();
	}

	private void initResourceTypeInfo(){
		mResourceTypeArray.clear();
		List<ResourceTypeInfo4> resourceTypeArray = mCompanyDetailInfo.getCore_attr();
		if(resourceTypeArray != null && resourceTypeArray.size() > 0){
			for(int index = 0; index < resourceTypeArray.size(); index++){
				if(resourceTypeArray.get(index).getIs_selected() == 1) {
					MultiItemBean bean = new MultiItemBean();
					bean.setName(resourceTypeArray.get(index).getClassname());
					bean.setId(String.valueOf(resourceTypeArray.get(index).getClassid()));
					bean.setSelected("0");
					mResourceTypeArray.add(bean);
				}
			}
		}
		Log.d(TAG, "mResourceTypeArray: " + mResourceTypeArray.toString());
	}

	private void addResourceTypeItem(List<Integer> select){
		if(select == null){
			return;
		}

		mResourceType.removeAllViews();
		LayoutInflater layoutInflater = LayoutInflater.from(getBaseContext());
		for(int index = 0; index < select.size(); index++){
			View itemLayout = layoutInflater.inflate(R.layout.item_search_bar, null);

			ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			params.setMargins(DisplayUtil.dp2px(getBaseContext(), 0),
					0,
					12,
					0);
			itemLayout.setLayoutParams(params);

			TextView text = itemLayout.findViewById(R.id.item_show_str);
			text.setText(mResourceTypeArray.get(select.get(index)).getName());
			itemLayout.setBackgroundResource(R.drawable.rounded_button);
			mResourceType.addView(itemLayout);
		}
	}

	private void showResourceTypeSelector(){
		MultiPickerConfig pickerConfig = new MultiPickerConfig.Builder()
				.title("选择类型")
				.build();
		pickerConfig.setTitleBackgroundColorStr("#FFD401");

		mResourceTypePickerView.setConfig(pickerConfig);
		mResourceTypePickerView.setOnMultiItemClickListener(new OnMultiPickerItemClickListener() {
			@Override
			public void onSelected(List<Integer> select) {
				super.onSelected(select);
				mSelectResource = select;
				addResourceTypeItem(select);
			}

			@Override
			public void onCancel() {
				super.onCancel();
			}
		});

		mResourceTypePickerView.showMultiPicker();
	}

	private boolean checkInputSituation(){
		if(mInputView.getNumber() == null || mInputView.getNumber().equals("") ||
				mHeft.getText().toString()  == null || mHeft.getText().toString().equals("") ||
				mSelectorTruck_cidKey == null || mSelectorTruck_cidKey.equals("") ||
				mSelectTruckCid == null || mSelectTruckCid.size() <= 0 ||
				mSelectResource == null || mSelectResource.size() <= 0 ||
				mContactInfo.getText().toString() == null || mContactInfo.getText().toString().equals("") ||
				mTelephoneInfo.getText().toString() == null || mTelephoneInfo.getText().toString().equals("")
		){
			return false;
		}

		return true;
	}

	private boolean checkFileUploadResult() {
		return mIvTransportLicenseUpload &&
				mIvEmployeeLicenseUpload &&
				mIvQualificationCertificateUpload &&
				mIvDriverLicenseUpload;
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.btn_action_commit){
			if(checkInputSituation() && checkFileUploadResult()) {
				executeCommitAction();
			}
			else{
				ToastUtils.showLongToast(this, "请填写完整的入驻信息");
			}
		}
		else if(v.getId() == R.id.ic_add_item){
			if(mContactExtra.getVisibility() == View.VISIBLE){
				ToastUtils.showLongToast(this, "最多添加两个联系人！");
			}
			else{
				mContactExtra.setVisibility(View.VISIBLE);
			}
		}
		else if(v.getId() == R.id.layout_car_type_select){
			showCarTypeSelector();
		}
		else if(v.getId() == R.id.layout_resource_type_selector){
			showResourceTypeSelector();
		}
		else if(v.getId() == R.id.layout_transport_license){
			mSelectType = SELECT_TRANSPORT_LICENSE;
			mBottomDialog.showDialog(CompanyAddTruckActivity.this);
		}
		else if(v.getId() == R.id.layout_employment_license){
			mSelectType = SELECT_EMPLOYEE_LICENSE;
			mBottomDialog.showDialog(CompanyAddTruckActivity.this);
		}
		else if(v.getId() == R.id.layout_qualification_certificate){
			mSelectType = SELECT_QUALIFICATION_CERTIFICATE;
			mBottomDialog.showDialog(CompanyAddTruckActivity.this);
		}
		else if(v.getId() == R.id.layout_driver_license){
			mSelectType = SELECT_DRIVER_LICENSE;
			mBottomDialog.showDialog(CompanyAddTruckActivity.this);
		}
		else{
			/**/
		}
	}

	private void executeSearchCompanyInfo(){
		HashMap<String, String> header = new HashMap<String, String>();
		header.put("token", AuthController.Instance().getToken());

		sendHttpRequest(
			IHttpRequest.RequestMethod.GET_METHOD,
			RemoteAPI.REQUEST_V1_USER_COMPANY + "/" + mCid,
			header,
			null,
			RemoteAPICmd.REQUEST_V1_USER_COMPANY);
	}

	private void executeCommitAction(){
		HashMap<String, String> header = new HashMap<String, String>();
		header.put("token", AuthController.Instance().getToken());

		JSONObject json = new JSONObject();
		try {
			json.put("cid", mCid);
			json.put("cname", mCname);
			json.put("classid", mHashMapCarTypeArray.get(mSelectorTruck_cidKey).get(mSelectTruckCid.get(0)).getId());
			json.put("truckno", mInputView.getNumber());
			json.put("main_pic", "reserved");
			json.put("area_id", "reserved");
			json.put("cname", "无所属公司");


			JSONArray array = new JSONArray();
			JSONObject object = new JSONObject();
			object.put("contact", mContactInfo.getText().toString());
			object.put("mobilephone", mTelephoneInfo.getText().toString());
			array.put(0,object);
			if (mContactInfoExtra.getText().toString() != null &&
					!mContactInfoExtra.getText().toString().equals("") &&
					mTelephoneInfoExtra.getText().toString() != null &&
					!mTelephoneInfoExtra.getText().toString().equals("")
			) {
				JSONObject object_extra = new JSONObject();

				object_extra.put("contact", mContactInfoExtra.getText().toString());
				object_extra.put("mobilephone", mTelephoneInfoExtra.getText().toString());

				array.put(1,object_extra);
			}

			json.put("contact_data", array);
			json.put("road_transport", mSelectPicture.get("transportLicense"));
			json.put("supercargo", mSelectPicture.get("employeeLicense"));
			json.put("qualification", mSelectPicture.get("qualificationCertificate"));
			json.put("driving_permit", mSelectPicture.get("driverLicense"));
			String core_attr = "";
			for(int index = 0; index < mSelectResource.size(); index++){
				core_attr += mResourceTypeArray.get(mSelectResource.get(index)).getName() + ",";
			}
			if(core_attr.length() > 0){
				core_attr = core_attr.substring(0, core_attr.length() - 1);
			}
			json.put("core_attr", core_attr);
			json.put("length", "3.8");
			json.put("weight", mHeft.getText().toString());
			Log.d(TAG, "param: " + json.toString());

			sendHttpJsonRequest(
					RemoteAPI.REQUEST_V1_USER_COMPANY_ADD_TRUCK,
					header,
					json,
					RemoteAPICmd.REQUEST_V1_USER_COMPANY_ADD_TRUCK);
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	@Override
	protected void onHttpResponse(int actionId, String response) {
		try {
			JSONObject res = new JSONObject(response);
			Gson gson = new Gson();
			if(actionId == RemoteAPICmd.REQUEST_V1_USER_COMPANY){
				Log.d(TAG, "onResponse: " + response);
				ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
				if(responseInfo != null && responseInfo.getStatus() == 1){
					mCompanyDetailInfo = gson.fromJson(res.getString("data"), CompanyDetailInfo.class);
					initResourceTypeInfo();
				}
			}
			else if(actionId == RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_TRANSPORT_LICENSE_OFFSET){
				Log.d(TAG, "onResponse: " + response);
				ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
				if(responseInfo != null && responseInfo.getStatus() == 1){
					stopAnimation(mIvTransportLicenseLoading);
					mIvTransportLicenseUpload = true;
					if(mSelectPicture.containsKey("transportLicense")){
						mSelectPicture.remove("transportLicense");
					}
					mSelectPicture.put("transportLicense", res.getJSONObject("data").getString("url"));
				}
			}
			else if(actionId == RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_EMPLOYEE_LICENSE_OFFSET){
				Log.d(TAG, "onResponse: " + response);
				ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
				if(responseInfo != null && responseInfo.getStatus() == 1){
					stopAnimation(mIvEmployeeLicenseLoading);
					mIvEmployeeLicenseUpload = true;
					if(mSelectPicture.containsKey("employeeLicense")){
						mSelectPicture.remove("employeeLicense");
					}
					mSelectPicture.put("employeeLicense", res.getJSONObject("data").getString("url"));
				}
			}
			else if(actionId == RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_QUALIFICATION_CERTIFICATE_OFFSET){
				Log.d(TAG, "onResponse: " + response);
				ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
				if(responseInfo != null && responseInfo.getStatus() == 1){
					stopAnimation(mIvQualificationCertificateLoading);
					mIvQualificationCertificateUpload = true;
					if(mSelectPicture.containsKey("qualificationCertificate")){
						mSelectPicture.remove("qualificationCertificate");
					}
					mSelectPicture.put("qualificationCertificate", res.getJSONObject("data").getString("url"));
				}
			}
			else if(actionId == RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_DRIVER_LICENSE_OFFSET){
				Log.d(TAG, "onResponse: " + response);
				ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
				if(responseInfo != null && responseInfo.getStatus() == 1){
					stopAnimation(mIvDriverLicenseLoading);
					mIvDriverLicenseUpload = true;
					if(mSelectPicture.containsKey("driverLicense")){
						mSelectPicture.remove("driverLicense");
					}
					mSelectPicture.put("driverLicense", res.getJSONObject("data").getString("url"));
				}
			}
			else if(actionId == RemoteAPICmd.REQUEST_V1_USER_COMPANY_ADD_TRUCK){
				Log.d(TAG, "onResponse: " + response);
				ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
				if(responseInfo != null && responseInfo.getStatus() == 1){
					String tid = res.getJSONObject("data").getString("tid");
					Intent intent = new Intent(this, CompanyAddTruckSuccessActivity.class);
					intent.putExtra("tid", tid);
					startActivity(intent);
				}
				else{
					ToastUtils.showLongToast(this, res.getString("message"));
				}
			}
			else{

			}
		}
		catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onHttpErrorResponse(int actionId, String response) {
		dismissLoadingDialog();
		if(actionId == RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_TRANSPORT_LICENSE_OFFSET){
			ToastUtils.showLongToast(this, "上传铁路运输证失败");
		}
		else if(actionId == RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_EMPLOYEE_LICENSE_OFFSET){
			ToastUtils.showLongToast(this, "上传押运员证失败");
		}
		else if(actionId == RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_QUALIFICATION_CERTIFICATE_OFFSET){
			ToastUtils.showLongToast(this, "上传危险品从业资格证失败");
		}
		else if(actionId == RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_DRIVER_LICENSE_OFFSET){
			ToastUtils.showLongToast(this, "上传机动车行驶证失败");
		}
		else if(actionId == RemoteAPICmd.REQUEST_V1_USER_COMPANY){
			ToastUtils.showLongToast(this, "添加车辆失败");
		}
		else{

		}
	}

	private void startAnimation(ImageView imageView){
		Window window = getWindow();
		WindowManager.LayoutParams params = window.getAttributes();
		params.gravity = Gravity.CENTER;
		window.setAttributes(params);

		Animation rotateAnimation = AnimationUtils.loadAnimation(getBaseContext(),
				R.anim.anim_circle_rotate);
		LinearInterpolator interpolator = new LinearInterpolator();
		rotateAnimation.setInterpolator(interpolator);
		imageView.startAnimation(rotateAnimation);
		imageView.setVisibility(View.VISIBLE);
	}

	private void stopAnimation(ImageView imageView){
		imageView.clearAnimation();
		imageView.setImageResource(R.drawable.ic_upload_done);
	}

	private void pushFileToRemote(String file, int action){
		HashMap<String, String> header = new HashMap<>();
		header.put("token", AuthController.Instance().getToken());

		sendHttpMultipartRequest(
				RemoteAPI.REQUEST_UPLOAD_FILE,
				header,
				null,
				file,
				action);
	}

	private void getPicFromAlbum() {
		Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
		photoPickerIntent.setType("image/*");
		startActivityForResult(photoPickerIntent, ALBUM_REQUEST_CODE);
	}

	private void getPicFromCamera(){
		mTempFile = new File(Environment.getExternalStorageDirectory() +
				File.separator + "vsiji" + File.separator
				+ "Pictures", System.currentTimeMillis() + ".jpg");
		//跳转到调用系统相机
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		//判断版本
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {   //如果在Android7.0以上,使用FileProvider获取Uri
			intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
			Uri contentUri = FileProvider.getUriForFile(this,
					"com.example.dangerouscargodriver.fileprovider",
					mTempFile);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
		} else {    //否则使用Uri.fromFile(file)方法获取Uri
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempFile));
		}

		startActivityForResult(intent, CAMERA_REQUEST_CODE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		switch (requestCode) {
			case CAMERA_REQUEST_CODE:   //调用相机后返回
				if (resultCode == RESULT_OK) {
					Bitmap bitmap = null;
					if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
						Uri contentUri = FileProvider.getUriForFile(this,
								"com.example.dangerouscargodriver.fileprovider",
								mTempFile);
						FileInputStream fis = null;
						try {
							fis = new FileInputStream(mTempFile);
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						}
						bitmap = BitmapFactory.decodeStream(fis);
						bitmap = ImageUtils.extractMiniThumb(bitmap, 232, 155);
					}
					else {
						bitmap = BitmapFactory.decodeFile(mTempFile.getPath());
					}
					if(mSelectType == SELECT_TRANSPORT_LICENSE) {
						mIvTransportLicense.setImageBitmap(bitmap);
						if(mSelectPicture.containsKey("transportLicense")){
							mSelectPicture.remove("transportLicense");
						}
						mSelectPicture.put("transportLicense", mTempFile.getPath());
						pushFileToRemote(mTempFile.getPath(),
								RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_TRANSPORT_LICENSE_OFFSET);
						startAnimation(mIvTransportLicenseLoading);
					}
					else if(mSelectType == SELECT_EMPLOYEE_LICENSE){
						mIvEmployeeLicense.setImageBitmap(bitmap);
						if(mSelectPicture.containsKey("employeeLicense")){
							mSelectPicture.remove("employeeLicense");
						}
						mSelectPicture.put("employeeLicense", mTempFile.getPath());
						pushFileToRemote(mTempFile.getPath(),
								RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_EMPLOYEE_LICENSE_OFFSET);
						startAnimation(mIvEmployeeLicenseLoading);
					}
					else if(mSelectType == SELECT_QUALIFICATION_CERTIFICATE){
						mIvQualificationCertificate.setImageBitmap(bitmap);
						if(mSelectPicture.containsKey("qualificationCertificate")){
							mSelectPicture.remove("qualificationCertificate");
						}
						mSelectPicture.put("qualificationCertificate", mTempFile.getPath());
						pushFileToRemote(mTempFile.getPath(),
								RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_QUALIFICATION_CERTIFICATE_OFFSET);
						startAnimation(mIvQualificationCertificateLoading);
					}
					else{
						mIvDriverLicense.setImageBitmap(bitmap);
						if(mSelectPicture.containsKey("driverLicense")){
							mSelectPicture.remove("driverLicense");
						}
						mSelectPicture.put("driverLicense", mTempFile.getPath());
						pushFileToRemote(mTempFile.getPath(),
								RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_DRIVER_LICENSE_OFFSET);
						startAnimation(mIvDriverLicenseLoading);
					}
				}
				break;
			case ALBUM_REQUEST_CODE:    //调用相册后返回
				if (resultCode == RESULT_OK) {
					Uri uri = intent.getData();
					String file = ImageUtils.getRealPathFromURI(uri, this);
					FileInputStream fis = null;
					try {
						fis = new FileInputStream(file);
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					}
					Bitmap bitmap = BitmapFactory.decodeStream(fis);
					bitmap = ImageUtils.extractMiniThumb(bitmap, 320, 160);
					if(mSelectType == SELECT_TRANSPORT_LICENSE) {
						mIvTransportLicense.setImageBitmap(bitmap);
						pushFileToRemote(file,
								RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_TRANSPORT_LICENSE_OFFSET);
						startAnimation(mIvTransportLicenseLoading);
					}
					else if(mSelectType == SELECT_EMPLOYEE_LICENSE){
						mIvEmployeeLicense.setImageBitmap(bitmap);
						if(mSelectPicture.containsKey("employeeLicense")){
							mSelectPicture.remove("employeeLicense");
						}
						mSelectPicture.put("employeeLicense", file);
						pushFileToRemote(file,
								RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_EMPLOYEE_LICENSE_OFFSET);
						startAnimation(mIvEmployeeLicenseLoading);
					}
					else if(mSelectType == SELECT_QUALIFICATION_CERTIFICATE){
						mIvQualificationCertificate.setImageBitmap(bitmap);
						if(mSelectPicture.containsKey("qualificationCertificate")){
							mSelectPicture.remove("qualificationCertificate");
						}
						mSelectPicture.put("qualificationCertificate", file);
						pushFileToRemote(file,
								RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_QUALIFICATION_CERTIFICATE_OFFSET);
						startAnimation(mIvQualificationCertificateLoading);
					}
					else{
						mIvDriverLicense.setImageBitmap(bitmap);
						if(mSelectPicture.containsKey("driverLicense")){
							mSelectPicture.remove("driverLicense");
						}
						mSelectPicture.put("driverLicense", file);
						pushFileToRemote(file,
								RemoteAPICmd.REQUEST_UPLOAD_FILE + ACTION_DRIVER_LICENSE_OFFSET);
						startAnimation(mIvDriverLicenseLoading);
					}
				}
				break;
		}
	}
}
